<template>
	<view>
		<cu-custom bgColor="bg-gradual-pink" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">编辑资料</block>
		</cu-custom>
		<form>
			<view class="cu-form-group">
				<view class="title">物料编号</view>
				<input placeholder="请输入物料编号" name="input" v-model="erpGoods.code" @input="onInput" auto-focus
					></input>
			</view>
			<view class="cu-form-group">
				<view class="title">物料名称</view>
				<input placeholder="用户名" name="input" v-model="erpGoods.name" disabled></input>
			</view>

			
			<view class="padding flex flex-direction">
				<button class="cu-btn bg-blue lg" @click="goGoodsList">提交</button>
			</view>



			<view class="padding flex flex-direction">
				<button class="cu-btn bg-blue lg" @click="scanBarcode">扫描</button>
			</view>
		</form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				index: -1,
				switchC: true,
				imgList: [],
				url: {
					getGoodsByNo: '/base/erpGoods/list',
					getByBarcode: '/moveorder/tslPdaMoveOrder/getByBarcode',
				},
				erpGoods: {
					code: '',
					name: '',
					noneSpec: '',
					categoryNo: 1,
					categoryName: ''
				},
			};
		},

		methods: {
			onInput(data) {
				// 扫描成功回调
				let barcode = data.target.value;
				console.log(barcode)
				let getByBarcodeUrl = this.url.getByBarcode + "?barcode=" + barcode;
				//根据条码查询移库单数据
				this.$http.get(getByBarcodeUrl).then(res => {
					console.log(res)
					this.$tip.loaded();
					if (res.data.success) {
						//判断移库单类型跳转不同页面
						let entity = res.data.result[0];
						//如果type为1，则跳转到移库列表
						if (entity.type === "1") {
							console.log("跳转移库单")
							uni.navigateTo({
								url: '/pages/scan/moveConfirm?item=' +
									encodeURIComponent(JSON.stringify(res.data.result))
							});
						} else if (entity.type === "0") {
							//如果type为0，则跳转到入库列表
							console.log("跳转入库确认单")
							uni.navigateTo({
								url: '/pages/scan/goodsList?item=' +
									encodeURIComponent(JSON.stringify(this.erpGoods))
							});
						} else {
							//暂不跳转
						}
						/* uni.navigateTo({
							url: '/pages/user/userdetail'
						}) */
					}
				}).catch(() => {
					this.$tip.loaded();
					this.$tip.error('提交失败')
				});
			},

			scanBarcode() {
				uni.scanCode({
					success: (res) => {
						// 扫描成功回调
						console.log('扫描结果：' + res.result);
						console.log('条码类型：' + res.scanType);
						this.erpGoods.code = res.result
						let getByBarcodeUrl = this.url.getByBarcode + "?barcode=" + res.result;
						//根据条码查询移库单数据
						this.$http.get(getByBarcodeUrl).then(res => {
							console.log(res)
							this.$tip.loaded();
							if (res.data.success) {
								//判断移库单类型跳转不同页面
								let entity = res.data.result[0];
								//如果type为1，则跳转到移库列表
								if (entity.type === "1") {
									console.log("跳转移库单")
									uni.navigateTo({
										url: '/pages/scan/moveConfirm?item=' +
											encodeURIComponent(JSON.stringify(res.data.result))
									});
								} else if (entity.type === "0") {
									//如果type为0，则跳转到入库列表
									console.log("跳转入库确认单")
									uni.navigateTo({
										url: '/pages/scan/goodsList?item=' +
											encodeURIComponent(JSON.stringify(this.erpGoods))
									});
								} else {
									//暂不跳转
								}
								/* uni.navigateTo({
									url: '/pages/user/userdetail'
								}) */
							}
						}).catch(() => {
							this.$tip.loaded();
							this.$tip.error('提交失败')
						});
						// 这里可以根据res.result来处理后续业务逻辑
					},
					fail: (err) => {
						// 扫描失败回调
						console.error('扫描失败：' + err);
					}
				});
			},


			goGoodsList() {
				console.log(this.erpGoods.code)
				if (this.erpGoods.code == '') {
					alert("请添加")
					return;
				}
				uni.navigateTo({
					url: '/pages/scan/goodsList?item=' + encodeURIComponent(JSON.stringify(this.erpGoods))
				});
			},
			onSubmit() {
				console.log('myform', this.erpGoods)
				this.$tip.loading();
				this.$http.put(this.url.getGoodsByNo, this.erpGoods).then(res => {
					console.log(res)
					this.$tip.loaded();
					if (res.data.success) {
						this.$tip.toast('提交成功')
						goGoodsList(item)
						/* uni.navigateTo({
							url: '/pages/user/userdetail'
						}) */
					}
				}).catch(() => {
					this.$tip.loaded();
					this.$tip.error('提交失败')
				});
			},
			DateChange(e) {
				this.myFormData.birthday = e.detail.value
			},
			SwitchC(e) {
				this.switchC = e.detail.value
			},
			ChooseImage() {
				var that = this;
				uni.chooseImage({
					count: 4, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						that.$http.upload(that.$config.apiUrl + that.uploadUrl, {
								filePath: res.tempFilePaths[0],
								name: 'file'
							})
							.then(res => {
								that.myFormData.avatar = res.data.message;
							})
							.catch(err => {
								that.$tip.error(err.data.message)
							});
						this.imgList = res.tempFilePaths
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '召唤师',
					content: '确定要删除这段回忆吗？',
					cancelText: '再看看',
					confirmText: '再见',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			}
		}
	}
</script>

<style>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}
</style>